﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSON;
using Facebook_Graph_Toolkit.FacebookObjects;

namespace Facebook_Graph_Toolkit.GraphApi {
    /// <summary>
    /// A Facebook group
    /// </summary>
    [Serializable]
    public class Group : GraphApiObject{
        internal Group(JsonObject JO) : base(JO){
        }
        /// <summary>
        /// Gets the public information of a Facebook Group
        /// </summary>
        /// <param name="GroupID">Group ID</param>
        public Group(string GroupID) : base(Helpers.WebResponseHelper.GetJsonFromFacebookObject(GroupID, null)) {
        }

        /// <summary>
        /// Gets the information of a Facebook Group
        /// </summary>
        /// <param name="GroupID">Group ID</param>
        /// <param name="AccessToken">Access Token</param>
        public Group(string GroupID, string AccessToken) : base(Helpers.WebResponseHelper.GetJsonFromFacebookObject(GroupID, AccessToken)) {
        }

        /// <summary>
        /// The group ID
        /// </summary>
        public string ID { get { return (string)data["id"]; } }
        /// <summary>
        /// The profile that created this group
        /// </summary>
        public NameIDPair Owner {
            get {
                return new NameIDPair((JsonObject)data["owner"]);
            }
        }
        /// <summary>
        /// The name of the group
        /// </summary>
        public string Name { get { return (string)data["name"]; } }
        /// <summary>
        /// A brief description of the group
        /// </summary>
        public string Description { get { return (string)data["description"]; } }
        /// <summary>
        /// The privacy setting of the group
        /// </summary>
        public Privacy GroupPrivacy {
            get {
                string privacy = (string)data["privacy"];
                switch (privacy) {
                    case "OPEN":
                        return Privacy.Open;
                    case "CLOSED":
                        return Privacy.Closed;
                    default:
                        return Privacy.Secret;
                }
            }
        }
        /// <summary>
        /// The URL for the group's icon
        /// </summary>
        public string IconUrl { get { return (string)data["icon"]; } }
        /// <summary>
        /// The last time the group was updated
        /// </summary>
        public DateTime UpdatedTime {
            get {
                return Helpers.Common.RFC3339ToDateTime((string)data["updated_time"]);
            }
        }

        /// <summary>
        /// Gets a Group's wall
        /// </summary>
        /// <returns>The group's wall</returns>
        public static IList<Post> GetFeed(string GroupID, string AccessToken, int? Offset, int? Limit) {
            return Helpers.ApiCaller.GetFeed(GroupID, AccessToken, Offset, Limit);
        }
        /// <summary>
        /// Gets the members of a Group
        /// </summary>
        /// <param name="GroupID">Group ID</param>
        /// <param name="AccessToken">Access Token</param>
        /// <param name="Offset">Number of members to skip before returning the first member</param>
        /// <param name="Limit">Maximum number of members returned</param>
        /// <returns>A list of members</returns>
        public static IList<GroupMember> GetMembers(string GroupID, string AccessToken, int? Offset, int? Limit) {
            return Helpers.ApiCaller.GetGroupMembers(GroupID, AccessToken, Offset, Limit);
        }
    }
}
